package thread.producterAndCustomer;

import javafx.beans.binding.When;

import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.UUID;

public class Customer implements Runnable {

    private Queue<UUID> queue;
    private Object lock;

    public Customer(Queue<UUID> queue, Object lock) {
        this.queue = queue;
        this.lock = lock;
    }

    public void run() {
        while (true) {
            UUID uuid = null;
            while (null == uuid) {
                synchronized (lock) {
                    if (queue.size() == Configer.min) {
                        try {
                            System.out.println("消费停止");
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        uuid = queue.poll();
                        if (queue.size() == Configer.startProduce) {
                            lock.notifyAll();
                        }
                    }
                }
            }
            System.out.println(uuid.toString() + "取出执行");
            try {
                Thread.sleep(new Random().nextInt(300));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
